package com.jstnvr.smartcampus.controller;

import com.jstnvr.smartcampus.entity.FaceImg;
import com.jstnvr.smartcampus.service.FaceImgService;
import com.jstnvr.smartcampus.utils.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/FaceImageHandle")
public class FaceImgController {
    /*
     * 人脸图片处理
     * */
    @Autowired
    private Environment env;

    @Autowired
    private FaceImgService faceImgService;
    /*
     *
     * 跳转到上传的方法
     * */

    @RequestMapping("/upload.html")
    public String upload() {
        return "fileUpload";
    }

    /*
     * 处理图片上传请求
     * */
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        String path = env.getProperty("images.save-path");
        String filename = FileUpload.writeUploadFile(file, path);

        String szUid = (String) request.getSession().getAttribute("szUid");
        System.out.println("登录用户的szUid:" + szUid);
        FaceImg faceImg = new FaceImg();
        faceImg.setSzuid(szUid);
        faceImg.setImgurl(filename);
        faceImg.setActive(0);
        /*插入图片信息*/
        faceImgService.insert(faceImg);
        request.setAttribute("fileName", filename);
        request.setAttribute("result", "图片上传成功");

        return "showImage";

    }

    /*
     * 人脸验证
     * */
    @RequestMapping("/checkFace")
    @ResponseBody
    public String checkFace(HttpServletRequest request) {

        String fileName = request.getParameter("fileName");
        String realPath = env.getProperty("images.save-path") + fileName;
        System.out.println("图片路径：" + realPath);

        /*这里要调用sdk将数据传递到人脸识别服务器，同时将图片和用户的状态进行改变*/
        return "图片校验成功！";
    }


}
